package Mysinglelist;


public class mysinglelist {

    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;

    public void creatList() {
        ListNode node1 = new ListNode(10);
        ListNode node2 = new ListNode(20);
        ListNode node3 = new ListNode(30);
        ListNode node4 = new ListNode(40);
        ListNode node5 = new ListNode(50);
        this.head = node1;
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
    }

    public void show() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public int size() {
        int count = 0;
        ListNode cur = head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    public void addFirst(int val) {
        ListNode newnode = new ListNode(val);
        newnode.next = head;
        head = newnode;

    }

    public void addEnd(int val) {
        ListNode newnode = new ListNode(val);
        ListNode cur = head;
        if (cur == null) {
            head = newnode;
            return;
        }
        //注意这里的判断条件，是要找到链表的最后一个结点，并不是像遍历一样
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = newnode;
    }

    public void addIndex(int index, int val) {
        if (index < 0 || index > size()) {
            System.out.println("位置不合法");
            return;
        }
        //头
        if (index == 0) {
            addFirst(val);
            return;
        }
        //尾
        if (index == size()) {
            addEnd(val);
            return;
        }
//        ListNode cur=head.next;
//        for(int i=1;i<index-1;i++){
//            cur=cur.next;
//        }
        ListNode cur = findnode(index);
        ListNode newnode = new ListNode(val);
        newnode.next = cur.next;
        cur.next = newnode;
    }

    //找到index下标结点的前一个
    private ListNode findnode(int index) {
        ListNode cur = head;

        while (index - 1 != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }

    public void remove(int index) {
        if (index < 0 || index >= size()) {
            System.out.println("位置不合法");
            return;
        }
        //删除头结点
        if (index == 0) {
            head = head.next;
            return;
        }
        //中间结点
        ListNode cur = findnode(index);
        cur.next = cur.next.next;

        //尾结点 把cur的地址域设为null
        if (index == size()) {
            cur.next = null;
        }
    }

    //删除值为key的第一个结点
    public void removekey(int key) {
        if (head == null) {
            return;
        }
        if (head.val == key) {
            head = head.next;
            return;
        }
        ListNode cur = searchprev(key);
        ListNode del = cur.next;
        cur.next = del.next;
    }

    private ListNode searchprev(int key) {
        ListNode cur = head;
        while (cur.next != null) {
            if (cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

}
